//
//  RBLoginRuleView.m
//  zhongzhenglu
//
//  Created by XL on 2022/7/5.
//

#import "RBLoginRuleView.h"
#import <YYLabel.h>
#import "NSMutableAttributedString+RBAdd.h"
#import <Masonry.h>
#import "RBDefine.h"
#import <UIColor+YYAdd.h>
#import "UIView+RBAdd.h"
#import "RBRequestHM.h"
#import <UIView+YYAdd.h>
#import "RBLoginRuleResponse.h"
#import "RBLoginRuleVC.h"

@interface RBLoginRuleView()

@property(nonatomic, strong)UIImageView *ruleSelectImgView;
@property(nonatomic, strong)YYLabel *ruleL;

@property(nonatomic, strong)RBRequestHM *hm;
@property(nonatomic, strong)RBLoginRuleResponse *response;

@end

@implementation RBLoginRuleView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        UIImageView *selectImgView = UIImageView.new;
        [selectImgView rbAddTapGesWithTarget:self action:@selector(selViewTapped)];
        [selectImgView extendResponseArea];
        self.ruleSelectImgView = selectImgView;
        [self addSubview:selectImgView];
        [selectImgView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(0);
            make.top.mas_equalTo(0);
            make.width.height.mas_equalTo(Zoom(19));
        }];
        
        YYLabel *ruleL = YYLabel.new;
        self.ruleL = ruleL;
        [self addSubview:ruleL];
        [ruleL mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.bottom.mas_equalTo(0);
            make.top.mas_equalTo(Zoom(4));
            make.left.mas_equalTo(Zoom(27));
        }];
        ruleL.numberOfLines = 0;
        ruleL.preferredMaxLayoutWidth = kRBMainScreenWidth - Zoom(10)*2 - Zoom(27);
        
        self.read = YES;
        
        [self getData];
    }
    return self;
}

- (void)setRuleContent
{
    __weak typeof(self) weakSelf = self;
    self.ruleL.attributedText = ({
        NSMutableAttributedString *stringM = NSMutableAttributedString.new;
        UIFont *font = [UIFont systemFontOfSize:Zoom(12)];
        CGFloat lineSpacing = Zoom(9);
        UIColor *grayColor = [UIColor colorWithHexString:@"#9A9A9A"];
        UIColor *blackColor = [UIColor colorWithHexString:@"#333333"];
        [stringM appendFont:font color:grayColor lineSpacing:lineSpacing text:@"已阅读并同意" highlighBgColor:nil highlightColor:nil tapAction:nil longPressAction:nil];
        for (NSInteger i = 0; i < weakSelf.response.data.count; i++)
        {
            RBLoginRuleModel *model = weakSelf.response.data[i];
            NSString *title = model.title;
            if (i != weakSelf.response.data.count - 1)
            {
                title = [title stringByAppendingString:@"、"];
            }
            [stringM appendFont:font color:blackColor lineSpacing:lineSpacing text:title highlighBgColor:nil highlightColor:nil tapAction:^(UIView * _Nonnull containerView, NSAttributedString * _Nonnull text, NSRange range, CGRect rect) {
                [weakSelf ruleTappedAtIndex:i];
            } longPressAction:nil];
        }
        stringM;
    });
}

#pragma mark - Network

- (void)getData
{
    __weak typeof(self) weakSelf = self;
    [self.hm requestWithParam:^(NSMutableDictionary *dicM) {
        
    } log:YES post:YES cancelLastRequest:YES fileParam:nil fileProgress:nil responseClass:RBLoginRuleResponse.class pathName:@"han_article_reg_list" completion:^(RBLoginRuleResponse *response) {
        if (response.succeed)
        {
            weakSelf.response = response;
            [weakSelf setRuleContent];
        }
    }];
}

#pragma mark - Event

- (void)selViewTapped
{
    self.read = !self.read;
}

- (void)ruleTappedAtIndex: (NSInteger)index
{
    RBLoginRuleVC *vc = RBLoginRuleVC.new;
    RBLoginRuleModel *model = self.response.data[index];
    vc.id = model.id;
    [self.viewController.navigationController pushViewController:vc animated:YES];
}

#pragma mark - Setter

- (void)setRead:(BOOL)read
{
    _read = read;
    self.ruleSelectImgView.image = [UIImage imageNamed:read? @"accountRuleSel": @"accountRuleUn"];
}

#pragma mark - Getter

- (RBRequestHM *)hm
{
    if (!_hm)
    {
        self.hm = RBRequestHM.new;
    }
    return _hm;
}

@end
